package com.qf.ssm.controller;


import com.github.pagehelper.PageInfo;
import com.qf.ssm.qo.CarInfoQo;
import com.qf.ssm.qo.CarRentQo;
import com.qf.ssm.qo.CarTypeQo;
import com.qf.ssm.result.ResponseResult;
import com.qf.ssm.service.CarManagementService;
import com.qf.ssm.vo.CarBrandVo;
import com.qf.ssm.vo.CarInfoVo;
import com.qf.ssm.vo.CarRentVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@Api(tags = "车辆管理接口")
@RequestMapping("/car/management")
public class CarManagementController {
    @Autowired
    private CarManagementService carManagementService;


    @PostMapping("/insert")
    @ApiOperation("增加车辆信息")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response =Integer.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<Integer> addCarInfoQo(@RequestBody CarInfoQo carInfoQo){
        return ResponseResult.success(carManagementService.saveCarInfo(carInfoQo));
    }





    @PostMapping("/list/{page}/{size}")
    @ApiOperation("模糊和条件和分页查询车辆信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "carTypeQo",value = "车型条件参数")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = CarInfoVo.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<PageInfo<CarInfoVo>> carInfoList( @ApiParam(name = "page",value = "页数",required = true)@PathVariable(value = "page")Integer page,
                                                        @ApiParam(name = "size",value = "条数",required = true)@PathVariable(value = "size")Integer size,
                                                        @RequestBody CarTypeQo carTypeQo){
        return ResponseResult.success(carManagementService.carInfoList(page,size,carTypeQo));
    }



    @PostMapping("/image/upload")
    @ApiOperation("上传车辆信息相关图片")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = String.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<String> upload(@RequestBody MultipartFile pic,HttpServletRequest request){
        String originalFilename = pic.getOriginalFilename();
        String realPath=request.getServletContext().getRealPath("WEB-INF/upload/carInfoImg");
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString()+substring;
        File dir = new File(realPath);
        if (!dir.exists()){
            dir.mkdirs();
        }
        try {
            pic.transferTo(new File(realPath,fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseResult.success(fileName);
    }



    @GetMapping("/image/download")
    @ApiOperation("图片下载回显")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response =File.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public void download(String pname, HttpServletResponse response,HttpServletRequest request){
        try {
            String realPath=request.getServletContext().getRealPath("WEB-INF/upload/carInfoImg/");
            //获取图片文件
            FileInputStream inputStream = new FileInputStream(new File(realPath+pname));
            //输出流
            ServletOutputStream outputStream = response.getOutputStream();

            response.setContentType("image/jpeg");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @PutMapping("/update/{path}")
    @ApiOperation("修改车辆信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "carInfoQo",value = "车辆信息参数")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = Integer.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<Integer> updateCarInfo(@RequestBody CarInfoQo carInfoQo,@ApiParam(value = "车辆ID")@PathVariable("path")Integer cid){
        return ResponseResult.success(carManagementService.modifyCarInfo(carInfoQo,cid));
    }

    @DeleteMapping("/delete/{path}")
    @ApiOperation("删除车辆信息")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = Integer.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<Integer> deleteCarInfo(@ApiParam(value = "车辆ID")@PathVariable("path") Integer cid){
        return ResponseResult.success(carManagementService.removeCarInfo(cid));
    }

    @PutMapping("/update/rent/status/{path}/{status}")
    @ApiOperation("修改车辆状态")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = Integer.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<Integer> updateRentStatus(@ApiParam(value = "车辆ID")@PathVariable("path") Integer cid,
                                                    @ApiParam(value = "车辆运营状态 0租凭中,1待租凭,2已下架")@PathVariable("status") Integer status){
        return ResponseResult.success(carManagementService.modifyStatus(cid,status));
    }


    @GetMapping("/brand/list/{page}/{size}")
    @ApiOperation("查询品牌与其款式")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "页数"),@ApiImplicitParam(name ="size",value = "条数")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = CarBrandVo.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<PageInfo<CarBrandVo>>brandList(@PathVariable("page") int page,
                                                         @PathVariable("size") int size){
        return  ResponseResult.success(carManagementService.brandList(page,size));
    }

    @GetMapping("/{path}")
    @ApiOperation("根据ID查询车辆")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = CarInfoVo.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<CarInfoVo>getCarInfo(@ApiParam("车辆ID")@PathVariable("path")Integer cid){
        return ResponseResult.success(carManagementService.queryCarInfo(cid));
    }


    @PostMapping("/rent/edit/{path}")
    @ApiOperation("编辑车辆的租金")
    @ApiImplicitParams({@ApiImplicitParam(name = "carRentQo",value = "租金信息")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = Integer.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<Integer>editCarRent(@ApiParam("车辆ID")@PathVariable("path") Integer cid, @RequestBody CarRentQo carRentQo){
        return ResponseResult.success(carManagementService.saveOrModifyCarRent(cid,carRentQo));
    }

    @GetMapping("/rent/{path}")
    @ApiOperation("根据车辆ID查询租金信息")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = CarRentVo.class), @ApiResponse(code = 4000, message = "未知错误)")})
    public ResponseResult<CarRentVo>getCarRent(@ApiParam("车辆ID")@PathVariable("path") Integer cid){
        return ResponseResult.success(carManagementService.queryCarRent(cid));
    }

}
